/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import mx.edu.itschapala.sistemas.septimo.control.ClienteLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVentana;

/**
 *
 * @author Estudiante
 */
public class RegistrarC extends javax.swing.JFrame {

    private ClienteLnLocal clienteControl;
    private TipoVentana tipoVentana;
    private Cliente cliente;
    /**
     * Creates new form RegistrarC
     */
    public RegistrarC(Cliente cliente, ClienteLnLocal clienteControl, TipoVentana tipoVentana) {
        this.cliente=cliente;
        this.clienteControl=clienteControl;
        this.tipoVentana=tipoVentana;
        initComponents();
        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                tituloC.setText("Registrar Cliente");
                break;
            case VENTANA_BUSCAR:
                tituloC.setText("Buscar por ID");
                boGuardar.setText("Aceptar");
                textcNombre.setEditable(false);
                textcTelefono.setEditable(false);
                textcDireccion.setEditable(false);
                textcSexo.setEditable(false);
                boCancelar.setVisible(false);
                break;
            case VENTANA_MODIFICAR:
                tituloC.setText("Modificar Cliente");
                //botonGuardar.setText("Buscar");
                textcId.setEditable(false);
                textcId.setText(cliente.getId()+"");
                textcNombre.setText(cliente.getNombre());
                textcTelefono.setText(cliente.getTelefono()+"");
                textcDireccion.setText(cliente.getDireccion());
                textcSexo.setText(String.valueOf(cliente.getSexo()));
                //botonCancelar.setVisible(false);
                break;
            case VENTANA_ELIMINAR:
                tituloC.setText("Eliminar Cliente");
                textcId.setEditable(false);
                textcId.setText(cliente.getId()+"");
                textcNombre.setText(cliente.getNombre());
                textcTelefono.setText(cliente.getTelefono()+"");
                textcDireccion.setText(cliente.getDireccion());
                textcSexo.setText(String.valueOf(cliente.getSexo()));
                boGuardar.setText("Eliminar");
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        textcId = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        textcNombre = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        textcTelefono = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        textcDireccion = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        textcSexo = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        tituloC = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        boGuardar = new javax.swing.JButton();
        boCancelar = new javax.swing.JButton();

        jLabel2.setText("jLabel2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mx/edu/itschapala/sistemas/septimo/imgs/abstract_3d_cubes.jpg"))); // NOI18N

        jPanel1.setOpaque(false);

        jPanel5.setOpaque(false);

        jPanel2.setOpaque(false);
        jPanel2.setLayout(new java.awt.GridLayout(6, 2, 2, 2));

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(204, 204, 204));
        jLabel3.setText("ID");
        jPanel2.add(jLabel3);

        textcId.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        textcId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textcIdKeyPressed(evt);
            }
        });
        jPanel2.add(textcId);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(204, 204, 204));
        jLabel4.setText("Nombre");
        jPanel2.add(jLabel4);

        textcNombre.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jPanel2.add(textcNombre);

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(204, 204, 204));
        jLabel5.setText("Telefono");
        jPanel2.add(jLabel5);

        textcTelefono.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jPanel2.add(textcTelefono);

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(204, 204, 204));
        jLabel6.setText("Direccion");
        jPanel2.add(jLabel6);

        textcDireccion.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jPanel2.add(textcDireccion);

        jLabel7.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(204, 204, 204));
        jLabel7.setText("Sexo");
        jPanel2.add(jLabel7);

        textcSexo.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jPanel2.add(textcSexo);

        jPanel3.setOpaque(false);

        tituloC.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
        tituloC.setForeground(new java.awt.Color(204, 204, 204));
        tituloC.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        tituloC.setText("jLabel2");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tituloC, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(tituloC)
                .addGap(25, 25, 25))
        );

        jPanel4.setOpaque(false);

        boGuardar.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        boGuardar.setText("Guardar");
        boGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boGuardarActionPerformed(evt);
            }
        });

        boCancelar.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        boCancelar.setText("Cancelar");
        boCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(boGuardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(boCancelar)
                .addGap(21, 21, 21))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boGuardar)
                    .addComponent(boCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 2, Short.MAX_VALUE))
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(32, Short.MAX_VALUE)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 336, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 372, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void boGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boGuardarActionPerformed
         switch (tipoVentana) {
            case VENTANA_REGISTRO:
                    Cliente p = new Cliente();
                    p.setId(Integer.parseInt(textcId.getText()));
                    p.setNombre(textcNombre.getText());
                    p.setTelefono(Long.parseLong(textcTelefono.getText()));
                    p.setDireccion(textcDireccion.getText());
                    p.setSexo( textcSexo.getText().charAt(0));
                    clienteControl.registrar(p);
                    vaciarCampos();
                    JOptionPane.showMessageDialog(null, "Guardado correctamente");
                break;
            case VENTANA_BUSCAR:
                this.dispose();
                break;
            
            case VENTANA_MODIFICAR:
                    p = new Cliente();
                    p.setId(Integer.parseInt(textcId.getText()));
                    p.setNombre(textcNombre.getText());
                    p.setTelefono(Long.parseLong(textcTelefono.getText()));
                    p.setDireccion(textcDireccion.getText());
                    p.setSexo(textcSexo.getText().charAt(0));
                    clienteControl.modificar(p);
                    vaciarCampos();
                    textcNombre.setEditable(false);
                    textcTelefono.setEditable(false);
                    textcDireccion.setEditable(false);
                    textcSexo.setEditable(false);
                    JOptionPane.showMessageDialog(null, "Modificado correctamente");
                    this.dispose();
                break;
             
            case VENTANA_ELIMINAR:
                    p = new Cliente();
                    p.setId(Integer.parseInt(textcId.getText()));
                    p.setNombre(textcNombre.getText());
                    p.setTelefono(Long.parseLong(textcTelefono.getText()));
                    p.setDireccion(textcDireccion.getText());
                    p.setSexo(textcSexo.getText().charAt(0));
                    clienteControl.eliminar(p);
                vaciarCampos();
                JOptionPane.showMessageDialog(null, "Eliminado correctamente");
                this.dispose();
                break;
                
            
        }
    }//GEN-LAST:event_boGuardarActionPerformed

    private void textcIdKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textcIdKeyPressed
         if(tipoVentana!=tipoVentana.VENTANA_REGISTRO){
           if(evt.getKeyCode()==KeyEvent.VK_ENTER){
               Cliente p=clienteControl.buscarPorCodigo(Integer.parseInt(textcId.getText()));
               if(p!=null){
                   textcNombre.setText(p.getNombre());
                   textcTelefono.setText(String.valueOf(p.getTelefono()));
                   textcDireccion.setText(p.getDireccion());
                   textcSexo.setText(p.getSexo()+"");
                   if(tipoVentana==TipoVentana.VENTANA_MODIFICAR){
                       textcNombre.setEditable(true);
                       textcTelefono.setEditable(true);
                       textcDireccion.setEditable(true);
                       textcSexo.setEditable(true);
                   }
                   if(tipoVentana==TipoVentana.VENTANA_ELIMINAR){
                       textcNombre.setText(p.getNombre());
                       textcTelefono.setText(String.valueOf(p.getTelefono()));
                       textcDireccion.setText(p.getDireccion());
                       textcSexo.setText(p.getSexo()+"");
                       
                       
                   }
               }
           }
       }
    }//GEN-LAST:event_textcIdKeyPressed

    private void boCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_boCancelarActionPerformed

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boCancelar;
    private javax.swing.JButton boGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTextField textcDireccion;
    private javax.swing.JTextField textcId;
    private javax.swing.JTextField textcNombre;
    private javax.swing.JTextField textcSexo;
    private javax.swing.JTextField textcTelefono;
    private javax.swing.JLabel tituloC;
    // End of variables declaration//GEN-END:variables

    private void vaciarCampos() {
        textcId.setText("");
        textcNombre.setText("");
        textcTelefono.setText("");
        textcDireccion.setText("");
        textcSexo.setText("");
    }
}
